package com.happyclub.lcy.service.impl;

import com.happyclub.lcy.dao.ITravel;
import com.happyclub.lcy.dao.ITravelCommont;
import com.happyclub.lcy.dao.ITravelCommontReply;
import com.happyclub.lcy.dao.ITravelUserLike;
import com.happyclub.lcy.service.ITravelService;
import com.happyclub.paul.service.PhotoAlbumService;
import com.happyclub.pojo.TbTravel;
import com.happyclub.pojo.TbTravelCommont;
import com.happyclub.pojo.TbTravelUserLike;
import com.happyclub.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
@Service
public class TravelServiceImpl implements ITravelService {

    @Autowired
    private ITravel iTravel;

    @Autowired
    private PhotoAlbumService photoAlbumService;

    @Autowired
    private ITravelUserLike iTravelUserLike;

    @Autowired
    private ITravelCommont iTravelCommont;

    @Autowired
    private ITravelCommontReply iTravelCommontReply;


    @Transactional
    @Override
    /*String userId ,String travelTitled, String travelContext, String location, String channel, String ip*/
    public JsonResult travels(TbTravel tbTravel, MultipartFile[] multipartFiles) {
        JsonResult jsonResult  = new JsonResult();
       /* String  uuid =  getUUID();*/
        try {
            /* uuid, userId, getDate(), travelContext, 0, location, channel, ip, 0, getDate(), getDate()*/
            if (0==multipartFiles.length) {
                TbTravel save = iTravel.save(tbTravel);
                if (save == null) {
                    jsonResult.setMsg("游记发表失败");
                    jsonResult.setErrorCode("400");
                }
            }else {
                    TbTravel save1 = iTravel.save(tbTravel);
                    photoAlbumService.uploadPhotoAlbumFile(tbTravel.getPhotoAlbumId(),multipartFiles,tbTravel.getUserId(),null);
                    if (save1 == null) {
                        jsonResult.setMsg("游记发表失败");
                        jsonResult.setErrorCode("400");
                    }
                }
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            jsonResult.setMsg(e.getMessage());
            jsonResult.setErrorCode("500");
            e.printStackTrace();
        }
        return jsonResult;
    }

    @Transactional
    @Override
    //通过游记id查找游记详细信息
    public JsonResult getTravelsInfo(String travelId) {
        JsonResult jsonResult  = new JsonResult();
        try {
            TbTravel tbTravelById = iTravel.findTbTravelById(travelId);
            if(tbTravelById==null){
                jsonResult.setMsg("该游记信息已经删除");
                jsonResult.setErrorCode("400");
            }else{
                iTravel.upReadNum(travelId);
                jsonResult.setResult(tbTravelById);
            }
        }catch (Exception e){
            jsonResult.setMsg(e.getMessage());
            jsonResult.setErrorCode("500");
            e.printStackTrace();
        }
        return jsonResult;
    }
    //用户点赞功能
    @Transactional
    @Override
    public JsonResult userLike(String travelId,String userId){
        JsonResult jsonResult  = new JsonResult();
        try {
            TbTravelUserLike like = iTravelUserLike.findTbTravelUserLikeByTravelIdAndUserId(travelId, userId);
            if(like==null){
                int i = iTravel.upTravelLike(travelId);
                int j = iTravelUserLike.insertTralelUserLike(travelId, userId, new Date());
                if(i == 1 && j == 1){
                    return jsonResult;
                }else {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    jsonResult.setErrorCode("400");
                    jsonResult.setMsg("点赞失败，请联系管理员");
                }
            }else{
                jsonResult.setErrorCode("400");
                jsonResult.setMsg("已经点过赞了");
            }
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            jsonResult.setMsg(e.getMessage());
            jsonResult.setErrorCode("500");
            e.printStackTrace();
        }
        return jsonResult;
    }
    //插入評論 //插入评论后在把这条信息带出来返回给页面
    @Transactional
    @Override
    public JsonResult travelComment(TbTravelCommont tbTravelCommont) {
        JsonResult jsonResult  = new JsonResult();
        try {
            TbTravelCommont save = iTravelCommont.save(tbTravelCommont);
            if(save!=null){
                jsonResult.setResult(save);
                return jsonResult;
            }
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            jsonResult.setMsg(e.getMessage());
            jsonResult.setErrorCode("500");
            e.printStackTrace();
        }
        return jsonResult;
    }
}
